package view
{
	import com.bit101.components.Component;
	import com.bit101.components.HBox;
	import com.bit101.components.VBox;
	
	import events.NumEvent;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	
	public class PricePanle extends Sprite
	{
		private var hbox:VBox;
		private var allPrice:TextField;
		private var priceAry:Array;
		private var shape:Shape;
		public function PricePanle()
		{
			this.y = 550;
			this.x = 170;
			init();
		}
		public function set source(value:XML):void{
			while(hbox.numChildren){
				hbox.removeChildAt(0);
			}
			priceAry = new Array;
			var i:int;
			for each(var xml:XML in value.price.item){
				var obj:Object = new Object;
				obj.name = String(xml.@name);
				obj.price = String(xml.@price);
				var price:PriceItem = new PriceItem(hbox);
				price.addEventListener(Event.CHANGE,numChange);
				price.source = obj;
				priceAry.push(price);
				i++;
			}
			if(i > 1)
				drawThis(i*30);
			//setTimeout(drawThis,10);
		}
		private function drawThis(h:Number):void{
			//trace(hbox.height)
			//var h:int = hbox.height - 36;
			h = (h-36)/2;
			shape.graphics.clear();
			shape.graphics.lineStyle(1,0x999999);
			shape.graphics.moveTo(0,0);
			shape.graphics.lineTo(10,5);
			shape.graphics.lineTo(10,5+h);
			shape.graphics.lineTo(18,13+h);
			shape.graphics.lineTo(10,18+h);
			shape.graphics.lineTo(10,18+2*h);
			shape.graphics.lineTo(0,23+2*h);
		}
		private function numChange(event:Event):void{
			var p:int;
			var ary:Array = new Array;
			for(var i:int;i<priceAry.length;i++){
				p += PriceItem(priceAry[i]).allPrice;
				ary.push(PriceItem(priceAry[i]).data);
			}
			allPrice.htmlText = "<font color='#656565' size='27'><b>" + p + "元</b></font>"
			var evt:NumEvent = new NumEvent(NumEvent.NUMEVENT);
			evt.num =  p;
			evt.ary = ary;
			this.dispatchEvent(evt);
			
		}
		private function init():void{
			priceAry = new Array;
			
			var txt:TextField = new TextField;
			txt.width = 70;
			txt.height = 30;
			txt.selectable = false;
			txt.htmlText = "<font color='#666666' size='14'><b>选择版型</b></font>";
			this.addChild(txt);
			
			hbox = new VBox(this,0,30);
			
			allPrice = new TextField();
			allPrice.x = 260;
			allPrice.y = 55;
			allPrice.htmlText = "<font color='#656565' size='27'><b>0元</b></font>"
			this.addChild(allPrice);
			
			shape = new Shape;
			this.addChild(shape);
			shape.x = 200;
			shape.y = 30;
			
			//hbox.addEventListener(Component.DRAW,drawThis);
			//drawThis();
		}
	}
}